import request from '@/utils/request'

// ==================== 聊天室管理 ====================

// 获取聊天室列表
export function getChatRooms(params) {
  return request({
    url: '/api/chat/rooms',
    method: 'get',
    params
  })
}

// 创建聊天室
export function createChatRoom(data) {
  return request({
    url: '/api/chat/rooms',
    method: 'post',
    data
  })
}

// 获取聊天室详情
export function getChatRoomDetail(roomId) {
  return request({
    url: `/api/chat/rooms/${roomId}`,
    method: 'get'
  })
}

// 加入聊天室
export function joinChatRoom(roomId) {
  return request({
    url: `/api/chat/rooms/${roomId}/join`,
    method: 'post'
  })
}

// 离开聊天室
export function leaveChatRoom(roomId) {
  return request({
    url: `/api/chat/rooms/${roomId}/leave`,
    method: 'post'
  })
}

// 获取聊天室成员
export function getChatRoomMembers(roomId, params) {
  return request({
    url: `/api/chat/rooms/${roomId}/members`,
    method: 'get',
    params
  })
}

// ==================== 消息管理 ====================

// 获取聊天消息
export function getChatMessages(roomId, params) {
  return request({
    url: `/api/chat/rooms/${roomId}/messages`,
    method: 'get',
    params
  })
}

// 发送消息
export function sendMessage(roomId, data) {
  return request({
    url: `/api/chat/rooms/${roomId}/messages`,
    method: 'post',
    data
  })
}

// 撤回消息
export function recallMessage(messageId) {
  return request({
    url: `/api/chat/messages/${messageId}/recall`,
    method: 'post'
  })
}

// 删除消息
export function deleteMessage(messageId) {
  return request({
    url: `/api/chat/messages/${messageId}`,
    method: 'delete'
  })
}

// 标记消息已读
export function markMessageRead(roomId, messageId) {
  return request({
    url: `/api/chat/rooms/${roomId}/messages/${messageId}/read`,
    method: 'post'
  })
}

// 获取未读消息数
export function getUnreadCount() {
  return request({
    url: '/api/chat/messages/unread-count',
    method: 'get'
  })
}

// ==================== 私聊管理 ====================

// 获取私聊列表
export function getPrivateChats(params) {
  return request({
    url: '/api/chat/private',
    method: 'get',
    params
  })
}

// 创建私聊
export function createPrivateChat(userId) {
  return request({
    url: '/api/chat/private',
    method: 'post',
    data: { userId }
  })
}

// 获取私聊消息
export function getPrivateMessages(chatId, params) {
  return request({
    url: `/api/chat/private/${chatId}/messages`,
    method: 'get',
    params
  })
}

// 发送私聊消息
export function sendPrivateMessage(chatId, data) {
  return request({
    url: `/api/chat/private/${chatId}/messages`,
    method: 'post',
    data
  })
}

// ==================== 文件上传 ====================

// 上传聊天文件
export function uploadChatFile(formData) {
  return request({
    url: '/api/chat/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 上传聊天图片
export function uploadChatImage(formData) {
  return request({
    url: '/api/chat/upload/image',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// ==================== 表情包管理 ====================

// 获取表情包列表
export function getEmojis() {
  return request({
    url: '/api/chat/emojis',
    method: 'get'
  })
}

// 获取自定义表情包
export function getCustomEmojis() {
  return request({
    url: '/api/chat/emojis/custom',
    method: 'get'
  })
}

// 上传自定义表情包
export function uploadCustomEmoji(formData) {
  return request({
    url: '/api/chat/emojis/custom',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// ==================== 聊天设置 ====================

// 获取聊天设置
export function getChatSettings() {
  return request({
    url: '/api/chat/settings',
    method: 'get'
  })
}

// 更新聊天设置
export function updateChatSettings(data) {
  return request({
    url: '/api/chat/settings',
    method: 'put',
    data
  })
}

// ==================== 好友管理 ====================

// 获取好友列表
export function getFriends(params) {
  return request({
    url: '/api/chat/friends',
    method: 'get',
    params
  })
}

// 添加好友
export function addFriend(userId, message) {
  return request({
    url: '/api/chat/friends',
    method: 'post',
    data: { userId, message }
  })
}

// 接受好友请求
export function acceptFriendRequest(requestId) {
  return request({
    url: `/api/chat/friend-requests/${requestId}/accept`,
    method: 'post'
  })
}

// 拒绝好友请求
export function rejectFriendRequest(requestId) {
  return request({
    url: `/api/chat/friend-requests/${requestId}/reject`,
    method: 'post'
  })
}

// 删除好友
export function deleteFriend(friendId) {
  return request({
    url: `/api/chat/friends/${friendId}`,
    method: 'delete'
  })
}

// 获取好友请求列表
export function getFriendRequests(params) {
  return request({
    url: '/api/chat/friend-requests',
    method: 'get',
    params
  })
}

// ==================== 群组管理 ====================

// 创建群组
export function createGroup(data) {
  return request({
    url: '/api/chat/groups',
    method: 'post',
    data
  })
}

// 获取我的群组
export function getMyGroups(params) {
  return request({
    url: '/api/chat/groups/my',
    method: 'get',
    params
  })
}

// 邀请用户加入群组
export function inviteToGroup(groupId, userIds) {
  return request({
    url: `/api/chat/groups/${groupId}/invite`,
    method: 'post',
    data: { userIds }
  })
}

// 退出群组
export function leaveGroup(groupId) {
  return request({
    url: `/api/chat/groups/${groupId}/leave`,
    method: 'post'
  })
}

// 踢出群成员
export function kickGroupMember(groupId, userId) {
  return request({
    url: `/api/chat/groups/${groupId}/members/${userId}`,
    method: 'delete'
  })
}

// ==================== 在线状态 ====================

// 获取在线用户列表
export function getOnlineUsers() {
  return request({
    url: '/api/chat/online-users',
    method: 'get'
  })
}

// 更新在线状态
export function updateOnlineStatus(status) {
  return request({
    url: '/api/chat/online-status',
    method: 'put',
    data: { status }
  })
}
